import ListNode from "./ListNode";

function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
    // 1.定义虚拟节点
    const dummy = new ListNode(0);
    // 让虚拟节点指向头节点
    dummy.next = head;

    // 2.定义快慢指针
    let slow = dummy;
    let fast = dummy;
    // 3.先让快指针移动n个位置
    for(let i = 0; i <= n; i++) {
        fast = fast.next!;
    }

    // 4.让快慢指针一起移动，直至快指针的值为null
    while(fast) {
        slow = slow.next!;
        fast = fast.next!;
    }

    // 5. 此时慢指针指向的位置为要删除节点的前一个节点
    slow.next = slow.next!.next

    return dummy.next;
};




export {}